#include "function_base_test.h"
#include "viml/compiler.h"
#include "viml/vm.h"
#include <gtest/gtest.h>
#include <chrono>

namespace viml {

/**
 * 函数调用性能测试
 */
class FunctionPerformanceTest : public FunctionCallTestBase {
protected:
    void SetUp() override {
        FunctionCallTestBase::SetUp();
    }
};

// FC-301: 测试函数调用开销
TEST_F(FunctionPerformanceTest, FunctionCallOverhead) {
    std::string source = R"(
function simple_func()
    return 42
endfunction

// 多次调用函数测量平均开销
let total = 0
let i = 1
while i <= 100
    let total = total + simple_func()
    let i = i + 1
endwhile
echo total
)";
    
    auto metrics = measurePerformance(source, "FunctionCallOverhead");
    
    // 验证执行时间在合理范围内（具体阈值可能需要根据实际环境调整）
    EXPECT_LT(metrics.executionTime, 100000); // 100ms以内
    EXPECT_GT(metrics.functionCalls, 0);
    EXPECT_GT(metrics.instructions, 0);
}

// FC-302: 测试递归性能
TEST_F(FunctionPerformanceTest, RecursivePerformance) {
    std::string source = R"(
function fibonacci(n)
    if n <= 1
        return n
    endif
    return fibonacci(n - 1) + fibonacci(n - 2)
endfunction

let result = fibonacci(10)  " 计算斐波那契数列第10项
echo result
)";
    
    auto metrics = measurePerformance(source, "RecursivePerformance");
    
    // 验证递归深度10内的性能
    EXPECT_LT(metrics.executionTime, 50000); // 50ms以内
    EXPECT_GT(metrics.functionCalls, 10);
}

// FC-303: 测试内存使用
TEST_F(FunctionPerformanceTest, MemoryUsage) {
    std::string source = R"(
function create_list()
    let lst = []
    let i = 1
    while i <= 100
        let lst = lst + [i]
        let i = i + 1
    endwhile
    return lst
endfunction

function process_list(lst)
    let sum = 0
    for item in lst
        let sum = sum + item
    endfor
    return sum
endfunction

let large_list = create_list()
let result = process_list(large_list)
echo result
)";
    
    auto metrics = measurePerformance(source, "MemoryUsage");
    
    // 验证内存使用在合理范围内
    EXPECT_LT(metrics.executionTime, 100000); // 100ms以内
    EXPECT_GT(metrics.instructions, 0);
}

// FC-304: 测试大对象参数传递
TEST_F(FunctionPerformanceTest, LargeArgumentPassing) {
    std::string source = R"(
function create_large_dict()
    let dict = {}
    let i = 1
    while i <= 100
        let dict[i] = "value_" .. i
        let i = i + 1
    endwhile
    return dict
endfunction

function count_dict_items(dict)
    let count = 0
    for key in keys(dict)
        let count = count + 1
    endfor
    return count
endfunction

let large_dict = create_large_dict()
let result = count_dict_items(large_dict)
echo result
)";
    
    auto metrics = measurePerformance(source, "LargeArgumentPassing");
    
    // 验证大对象参数传递性能
    EXPECT_LT(metrics.executionTime, 100000); // 100ms以内
}

// FC-305: 测试高频函数调用
TEST_F(FunctionPerformanceTest, HighFrequencyCalls) {
    std::string source = R"(
function add(a, b)
    return a + b
endfunction

let total = 0
" 高频调用函数1000次
let i = 1
while i <= 1000
    let total = total + add(i, 1)
    let i = i + 1
endwhile
echo total
)";
    
    auto metrics = measurePerformance(source, "HighFrequencyCalls");
    
    // 验证1000次调用在100毫秒内完成
    EXPECT_LT(metrics.executionTime, 100000); // 100ms以内
    EXPECT_GE(metrics.functionCalls, 1000);
}

// FC-306: 测试尾递归优化（如果实现）
TEST_F(FunctionPerformanceTest, TailRecursionOptimization) {
    std::string source = R"(
function tail_recursive_sum(n, acc)
    if n <= 0
        return acc
    endif
    return tail_recursive_sum(n - 1, acc + n)
endfunction

let result = tail_recursive_sum(100, 0)
echo result
)";
    
    auto metrics = measurePerformance(source, "TailRecursionOptimization");
    
    // 验证尾递归性能
    EXPECT_LT(metrics.executionTime, 50000); // 50ms以内
}

// FC-307: 测试函数调用与内置操作性能对比
TEST_F(FunctionPerformanceTest, FunctionVsBuiltinPerformance) {
    std::string source = R"(
function custom_add(a, b)
    return a + b
endfunction

let total1 = 0
let total2 = 0

" 测试自定义函数性能
let i = 1
while i <= 100
    let total1 = total1 + custom_add(i, 1)
    let i = i + 1
endwhile

" 测试内置操作性能
let j = 1
while j <= 100
    let total2 = total2 + (j + 1)
    let j = j + 1
endwhile

echo total1
echo total2
)";
    
    auto metrics = measurePerformance(source, "FunctionVsBuiltinPerformance");
    
    // 验证性能差异在合理范围内
    EXPECT_LT(metrics.executionTime, 100000); // 100ms以内
}

} // namespace viml